package com.lbd99.data.lbd.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lbd99.data.lbd.dao.TIfipLogMapper;
import com.lbd99.data.lbd.dao.TIfivLogOrdinaryMapper;
import com.lbd99.data.lbd.dao.TOrderMapper;
import com.lbd99.data.lbd.dao.TPublishIfipMapper;
import com.lbd99.data.lbd.dao.TUserMapper;
import com.lbd99.data.lbd.dao.TUserOrderRelationMapper;
import com.lbd99.data.lbd.dao.TWalletMapper;
import com.lbd99.data.lbd.dao.TappealMapper;
import com.lbd99.data.lbd.vo.TIfipLog;
import com.lbd99.data.lbd.vo.TIfivLogOrdinary;
import com.lbd99.data.lbd.vo.TOrder;
import com.lbd99.data.lbd.vo.TPublishIfip;
import com.lbd99.data.lbd.vo.TUser;
import com.lbd99.data.lbd.vo.TUserOrderRelation;
import com.lbd99.data.lbd.vo.TWallet;
import com.lbd99.data.lbd.vo.Tappeal;

@Service
public class TOrderService {
	@Autowired
	private TOrderMapper tOrderMapper;
	
	@Autowired
	private TappealMapper tappealMapper;
	
	@Autowired
	private TUserOrderRelationMapper tUserOrderRelationMapper;
	
	@Autowired
	private TUserMapper tUserMapper;
	
	@Autowired
	private TUserService tOrderService;
	
	@Autowired
	private TWalletMapper tWalletMapper;
	
	@Autowired
	private TIfivLogOrdinaryMapper tIfivLogOrdinaryMapper;
	
	@Autowired
	private TIfipLogMapper tIfipLogMapper;
	
	@Autowired
	private TPublishIfipMapper tPublishIfipMapper;
	
	public List<Map<String, Object>> selectListMap(Map<String, Object> map) {
		return tOrderMapper.selectListByMap(map);
	}
	
	
	public List<Map<String, Object>> getBusinessList(Map<String, Object> map) {
		return tOrderMapper.getBusinessList(map);
	}


	public List<Map<String, Object>> selectOrderRelationListByMap(Map<String, Object> map) {
		return tOrderMapper.selectOrderRelationListByMap(map);
	}


	public List<Map<String, Object>> selectOrderRelationListToday() {
		return tOrderMapper.selectOrderRelationListToday();
	}


	public List<Map<String, Object>> selectOrderRelationListWeek(Map<String, Object> map) {
		return tOrderMapper.selectOrderRelationListWeek(map);
	}

	public Map<String, Object> handlerAppeal(Integer tAppealId, Integer status) {
		Tappeal tappeal = tappealMapper.selectByPrimaryKey(tAppealId);//申诉信息
		TOrder order = tOrderMapper.selectByOrderNo(tappeal.getOrderCode());//订单信息
		tappeal.setStatus(status);
		tappealMapper.updateByPrimaryKey(tappeal);
		
		TUser user =tUserMapper.selectByPrimaryKey(tappeal.getUserId());//买家
		TUser otherUser = tUserMapper.selectByPrimaryKey(tappeal.getOtherUserId());//卖家
		TWallet	wallet=tWalletMapper.selectByUserId(tappeal.getUserId()); //买家
		TWallet walletOther=tWalletMapper.selectByUserId(tappeal.getOtherUserId());//卖家
		
		if(status==1){//申诉通过，（模拟卖家确认收款）
            //卖方
            TUserOrderRelation record=new TUserOrderRelation();
            record.setUserId(tappeal.getOtherUserId());
            record.setOrderId(order.getId());
            record.setStatus(4);
            record.setBtn(2);
            tUserOrderRelationMapper.updateByUserIdAndOrderId(record);
           //买方
            TUserOrderRelation recordOther=new TUserOrderRelation();
            recordOther.setUserId(tappeal.getUserId());
            recordOther.setOrderId(order.getId());
            recordOther.setStatus(4);
            recordOther.setBtn(2);
            tUserOrderRelationMapper.updateByUserIdAndOrderId(recordOther);

           //------------钱包变化-----------------------start----------------------------
            //卖方
            TWallet recordRecord=new TWallet();
            recordRecord.setUserId(tappeal.getOtherUserId());
            //recordRecord.setIfip(wallet.getIfip()-order.getTradeNum());
            //recordRecord.setIfiv();
            recordRecord.setUpdateBy("-1");
            recordRecord.setUpdateTime(new Date());
            recordRecord.setMonthDeal(new BigDecimal(wallet.getMonthDeal().doubleValue()+order.getTradeNum()));
            tWalletMapper.updateByUserId(recordRecord);
           //买方
            TWallet walletOtherRecord=new TWallet();
            walletOtherRecord.setUserId(tappeal.getUserId());
            walletOtherRecord.setIfip(new BigDecimal(wallet.getIfip().doubleValue()+order.getTradeNum()));
            walletOtherRecord.setIfiv(new BigDecimal(wallet.getIfiv().doubleValue()));
            walletOtherRecord.setUpdateBy("-1");
            walletOtherRecord.setUpdateTime(new Date());
            walletOtherRecord.setMonthDeal(new BigDecimal(wallet.getMonthDeal().doubleValue()+order.getTradeNum()));
            tWalletMapper.updateByUserId(walletOtherRecord);
           //------------钱包变化-----------------------end----------------------------

            //------------矿晶日志变化-----------------------start----------------------------
            //卖方
            TIfipLog record8=new TIfipLog();
            record8.setUpdateTime(new Date());
            record8.setCreateTime(new Date());
            record8.setUpdateBy("-1");
            record8.setIfip(new BigDecimal(order.getTradeNum()));
            record8.setCreateBy("-1");
            //0-转出，1-转入，2-买入，3-卖出，4-兑换,5-变更ifip
            record8.setType(3);
            record8.setUid(otherUser.getUid());
            record8.setUserId(otherUser.getId());
            tIfipLogMapper.insertSelective(record8);
           //买方
            TIfipLog record9=new TIfipLog();
            record9.setUpdateTime(new Date());
            record9.setCreateTime(new Date());
            record9.setUpdateBy(user.getPhone());
            record9.setIfip(new BigDecimal(order.getTradeNum()));
            record9.setCreateBy(user.getPhone());
            //0-转出，1-转入，2-买入，3-卖出，4-兑换,5-变更ifip
            record9.setType(2);
            record9.setUid(user.getUid());
            record9.setUserId(user.getId());
            tIfipLogMapper.insertSelective(record9);
           //------------矿晶日志变化-----------------------end----------------------------

            //------------矿场普通记录日志变化-----------------------start----------------------------
           //买方
            /*TIfivLogOrdinary record10=new TIfivLogOrdinary();
            record10.setUserId(user.getId());
            record10.setUid(user.getUid());
            //类型(0-转出获得，1-流通获得，2-买入获得)
            record10.setType(2);
            record10.setCreateTime(new Date());
            record10.setIfiv(new BigDecimal(order.getTradeNum()*0.2));
            record10.setStatus(0);
            record10.setUpdateTime(new Date());
            record10.setCreateBy(user.getPhone());
            record10.setUpdateBy(user.getPhone());
            tIfivLogOrdinaryMapper.insertSelective(record10);*/
            //------------矿场普通记录日志变化-----------------------end----------------------------
            
            //统计被申诉者次数(大于3次限制转账限制买卖)
            int num = tappealMapper.getOtherUserCount(tappeal.getOtherUserId());
            if(num>=3){
            	//大于3次对该用户限制转账限制买卖
            	/*tUserMapper.updateUserStatus(-1, tappeal.getOtherUserId());*/
            	tOrderService.updateConfinedStatus(tappeal.getOtherUserId(), "confined_trade", 1);
            	tOrderService.updateConfinedStatus(tappeal.getOtherUserId(), "confined_transfer", 1);
            }
		}else if(status==-1){//驳回,模拟用户取消订单的操作
			/*TUser othertUser =tUserMapper.selectByPrimaryKey(tappeal.getUserId());//买家	（买方是other）
			 *TUser tUser = tUserMapper.selectByPrimaryKey(tappeal.getOtherUserId());//卖家*/
			
			//买方
            TUserOrderRelation record=new TUserOrderRelation();
            record.setUserId(user.getId());
            record.setOrderId(order.getId());
            record.setStatus(3);
            record.setBtn(-1);
            tUserOrderRelationMapper.updateByUserIdAndOrderId(record);

            //卖方
            TUserOrderRelation recordOther=new TUserOrderRelation();
            recordOther.setUserId(otherUser.getId());
            recordOther.setOrderId(order.getId());
            recordOther.setStatus(3);
            recordOther.setBtn(-1);
            tUserOrderRelationMapper.updateByUserIdAndOrderId(recordOther);
            
            //=========================================================================//
            //取消订单以后需要把TPublishIfip上架，ifip加上去
            TPublishIfip tPublishIfip = tPublishIfipMapper.selectByPrimaryKey(order.getPublishIfipId());
            tPublishIfip.setStatus(0);
            
            tPublishIfip.setNum(new BigDecimal(order.getTradeNum()+tPublishIfip.getNum().doubleValue()));
            tPublishIfipMapper.updateByPrimaryKeySelective(tPublishIfip);
            //=========================================================================//
            
            /*//退还卖家矿晶到他的钱包
            //卖家
            TWallet walletOtherRecord=new TWallet();
            walletOtherRecord.setUserId(otherUserId);
            walletOtherRecord.setIfip(walletOther.getIfip()+order.getTradeNum());
           // walletOtherRecord.setIfiv(walletOther.getIfiv()+order.getTradeNum()*0.2);
            walletOtherRecord.setUpdateBy(phone);
            walletOtherRecord.setUpdateTime(new Date());
           //  walletOtherRecord.setMonthDeal(walletOther.getMonthDeal()+order.getTradeNum());
            tWalletMapper.updateByUserId(walletOtherRecord);*/

            //修改别人的状态
		}
		Map<String,Object> resultMap=new HashMap<String,Object>();
		resultMap.put("isSuc",true);
		resultMap.put("msg","操作成功");
        return  resultMap;
	}
}
